
package cn.xxl.cordova.uimode;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

import android.content.Context;
import android.content.res.Configuration;
import android.app.UiModeManager;

public class UiModePlugin extends CordovaPlugin {

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals("isTV")) {
            JSONObject r = new JSONObject();
            r.put("isTV", this.isTV());
            callbackContext.success(r);
            return true;
        }

        return false;
    }

    private boolean isTV () {
        UiModeManager manager = (UiModeManager) this.cordova.getActivity().getSystemService(Context.UI_MODE_SERVICE);
        if (manager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
            return true;
        } else {
            return false;
        }
    }
}